package com.symagic.game;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONStringer;

import android.widget.Toast;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.OnActionCompleted;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.FadeTo;
import com.badlogic.gdx.scenes.scene2d.actions.MoveTo;
import com.badlogic.gdx.scenes.scene2d.actions.Parallel;
import com.badlogic.gdx.scenes.scene2d.actions.RotateTo;
import com.badlogic.gdx.scenes.scene2d.actions.ScaleTo;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.symagic.GameActivity;
import com.symagic.gesture.SymagicGestureLogic;
import com.symagic.gesture.SymagicGestureView;
import com.symagic.gesture.SymagicStage;



public class SymagicGame extends SymagicScreen {

	

	private static final int ENLARGE_PREDICTION_INDEX = 10;
	private static final double RESTRAIN_INDEX = 0.2;
	private static final int ELEMENT_NUM = 5;
	private int[] myAttribute = new int[ELEMENT_NUM];
	private int[] enemyAttribute = new int[ELEMENT_NUM];

	private double[] elementScoreArray = new double[ELEMENT_NUM]; 

	private SpriteBatch mSpriteBatch;
	private Texture backgroundTexture;
	private Texture buttonTexture;
	private Texture texturePlayer1;
	private Texture texturePlayer2;
	private Texture textureHealthBarsJingofPlayer1;
	private Texture textureHealthBarsShenofPlayer1;
	private Texture textureHealthBarsQiofPlayer1;
	private Texture textureHealthBarsJingofPlayer2;
	private Texture textureHealthBarsShenofPlayer2;
	private Texture textureHealthBarsQiofPlayer2;
	private Texture textureHealthBarsBackground;
	private SymagicStage mStage;
	private Button buttonBack;
	private Texture textureStar;
	private final static float ANIMATION_DURATION = 4f;
	private Actor player1;
	private Actor player2;
	private Image imagePlayer3;
	private Image imagePlayer4;
	private Image imageHealthBarsJingofPlayer1;
	private Image imageHealthBarsShenofPlayer1;
	private Image imageHealthBarsQiofPlayer1;
	private Image imageHealthBarsJingofPlayer2;
	private Image imageHealthBarsShenofPlayer2;
	private Image imageHealthBarsQiofPlayer2;
	private Image imageHealthBarsBackgroundJing1;
	private Image imageHealthBarsBackgroundShen1;
	private Image imageHealthBarsBackgroundQi1;
	private Image imageHealthBarsBackgroundJing2;
	private Image imageHealthBarsBackgroundShen2;
	private Image imageHealthBarsBackgroundQi2;
	private char attackChar = 'w';
	private int totalDamage;
	private boolean attackComplete=true;

	// the scale float
	private float xScaleRadio;
	private float yScaleRadio;

	// the gesture drawer actor
	private SymagicGestureView gestureDrawPane;

	// Gesture logic
	SymagicGestureLogic gestureLogic = null;

	// It is used to calculate the Damage of a single element
	public int calculateSingleElmentDamage(int elementIndex,
			int attributeValue, int attributeValueOfEnemy,
			int weakenedAttributeOfEnemy, int attributeOfEnemyWeakenUs) {
		double singleElementDamage = (((attributeValue - attributeValueOfEnemy) > 0 ? (attributeValue - attributeValueOfEnemy)
				: 0)
				+ ((attributeValue - weakenedAttributeOfEnemy
						* (0.9 - RESTRAIN_INDEX)) > 0 ? (attributeValue - weakenedAttributeOfEnemy)
						: 0) - ((attributeOfEnemyWeakenUs
				* (1 + RESTRAIN_INDEX) - attributeValue) > 0 ? (attributeOfEnemyWeakenUs - attributeValue)
					: 0))
				* ENLARGE_PREDICTION_INDEX * elementScoreArray[elementIndex];
		return (int) singleElementDamage;

	}

	public int calculateTheTotalDamage() {
		int myGoldDamage = calculateSingleElmentDamage(0, myAttribute[0],
				enemyAttribute[0], enemyAttribute[1], enemyAttribute[3]);
		int myWoodDamage = calculateSingleElmentDamage(1, myAttribute[1],
				enemyAttribute[1], enemyAttribute[4], enemyAttribute[0]);
		int myWaterDamage = calculateSingleElmentDamage(2, myAttribute[2],
				enemyAttribute[2], enemyAttribute[3], enemyAttribute[4]);
		int myFireDamage = calculateSingleElmentDamage(3, myAttribute[3],
				enemyAttribute[3], enemyAttribute[0], enemyAttribute[2]);
		int myEarthDamage = calculateSingleElmentDamage(4, myAttribute[4],
				enemyAttribute[4], enemyAttribute[2], enemyAttribute[1]);
		return (myGoldDamage + myWoodDamage + myWaterDamage + myFireDamage + myEarthDamage);
	}

	// Construct method
	public SymagicGame(Game game) {
		super(game);
	}

	private class gestureDetectorListener implements GestureListener {

		public boolean fling(float arg0, float arg1) {
			// TODO Auto-generated method stub
			
			//when the user is drawing the gesture
			if( gestureLogic.getGestureDrawingState() )
				return false;

			// the speed of your figure slide up over the screen
			if (arg1 <= -800) {

				ArrayList<SymagicGestureView.SymagicGestureViewHistory> history = gestureDrawPane
						.getRecognizeHistory();
				if (history.size() < 0)
					return false;
				for (int i = 0; i < history.size(); i++) {

					if (history.get(i).getName().equals("gold")) {
						elementScoreArray[0] += history.get(i).getScore();
					} else if (history.get(i).getName().equals("wood")) {
						elementScoreArray[1] += history.get(i).getScore();
					} else if (history.get(i).getName().equals("water")) {
						elementScoreArray[2] += history.get(i).getScore();
					} else if (history.get(i).getName().equals("fire")) {
						elementScoreArray[3] += history.get(i).getScore();
					} else if (history.get(i).getName().equals("earth")) {
						elementScoreArray[4] += history.get(i).getScore();
					}
				}

				totalDamage = calculateTheTotalDamage();
				for (int i = 0; i < elementScoreArray.length; i++) {
					elementScoreArray[i] = 0;
				}

				// send total damage to the othe player
				sendDamage(totalDamage);


				totalDamage = 50;
				if(attackComplete == true){
					
				attackComplete = false;
				textureStar = new Texture(Gdx.files.internal(game.getActivity()
						.attackway(attackChar)));

				textureStar.setFilter(TextureFilter.Linear,
						TextureFilter.Linear);

				float maxwidth = Gdx.graphics.getWidth()
						- textureStar.getWidth() * xScaleRadio;
				float maxheight = Gdx.graphics.getHeight()
						- textureStar.getHeight() * yScaleRadio;


				imagePlayer3 = new Image("star", textureStar);
				imagePlayer3.x = 0 * xScaleRadio;
				imagePlayer3.y = 0 * yScaleRadio;
				imagePlayer3.width = imagePlayer3.width * xScaleRadio;
				imagePlayer3.height = imagePlayer3.height * yScaleRadio;
				Action moveAction = MoveTo.$(290 * xScaleRadio,
						635 * yScaleRadio, ANIMATION_DURATION / 2);
				Action rotateAction = RotateTo.$(360, ANIMATION_DURATION);
				Action fadeAction2 = FadeTo.$(0f, 1f); // disappear in 1s
				fadeAction2.setCompletionListener(new OnActionCompleted() {
					public void completed(Action arg0) {
						// TODO Auto-generated method stub

						Action scaleActionHealthBarsJingofPlayer2 = ScaleTo.$(
								(1 - 60 / 100.0f), 1f, 0);
						imageHealthBarsJingofPlayer2
								.action(scaleActionHealthBarsJingofPlayer2);
						attackComplete = true;
						imagePlayer3.remove();

					}
				});
				imagePlayer3.action(Parallel.$(moveAction, rotateAction,
						Delay.$(fadeAction2, ANIMATION_DURATION / 2)));
				mStage.addActor(imagePlayer3);

			}

			}
			return false;
		}

		private void sendDamage(int damage) {
			// TODO Auto-generated method stub
			int totalDamage = damage;
			JSONStringer attackJSON = new JSONStringer();
			try {
				attackJSON.object();
				attackJSON.key("msgTypes");
				attackJSON.value(GameActivity.ATTACK_MSG);
				attackJSON.key("totalDamage");
				attackJSON.value(totalDamage);
				attackJSON.endObject();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			byte[] buffer = attackJSON.toString().getBytes();
			game.getActivity().getCommunicator().write(buffer);
		}

		public boolean longPress(int arg0, int arg1) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean pan(int arg0, int arg1, int arg2, int arg3) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean tap(int arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean touchDown(int arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean zoom(float arg0, float arg1) {
			// TODO Auto-generated method stub
			return false;
		}

	}

	
	//player actor
	public class PlayerActor extends Actor {
	    
		Texture texture;
		SymagicGame pointer;
		
		@Override
		public void draw(SpriteBatch batch, float arg1) {
			// TODO Auto-generated method stub
			 batch.draw(texture, this.x, this.y);
		}

		@Override
		public Actor hit(float arg0, float arg1) {
			// TODO Auto-generated method stub
			if (arg0 > 0 && arg0 < width) if (arg1 > 0 && arg1 < height) {
				game.setScreen(new AttributeScreen(game,pointer));
				return this;
			}
				
			return null;
		}

		@Override
		public boolean touchDown(float arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
			return false;
		}

		@Override
		public void touchDragged(float arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void touchUp(float arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}
		
		public PlayerActor(String name,Texture texture,SymagicGame pointer){
			 super(name);
			 this.texture=texture;
			 this.pointer=pointer;
			 this.height = texture.getHeight(); 
		     this.width = texture.getWidth();
		     //this.touchable=true;
		}

	}
	
	public void show() {

		// calculate the scale radio
		xScaleRadio = Gdx.graphics.getWidth() / 480.0f;
		yScaleRadio = Gdx.graphics.getHeight() / 800.0f;

		// Build the gestureLogic
		gestureLogic = new SymagicGestureLogic(game.getActivity());

		// set the thrashold
		gestureLogic.setSocreThrashold(2.0f);

		mSpriteBatch = new SpriteBatch();
		backgroundTexture = new Texture(
				Gdx.files.internal("gameBackground.png"));
		mStage = new SymagicStage(Gdx.graphics.getWidth(),
				Gdx.graphics.getHeight(), true, new gestureDetectorListener());

		// Get the picture resources of Player1 and Player2
		texturePlayer1 = new Texture(Gdx.files.internal("player1.png"));
		texturePlayer1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		texturePlayer2 = new Texture(Gdx.files.internal("player2.png"));
		texturePlayer2.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		// Get the texture of Health Bars

		textureHealthBarsBackground = new Texture(
				Gdx.files.internal("HealthBarsBackground.png"));
		textureHealthBarsBackground.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);
		textureHealthBarsJingofPlayer1 = new Texture(
				Gdx.files.internal("HealthBarsJing.png"));
		textureHealthBarsJingofPlayer1.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);
		textureHealthBarsQiofPlayer1 = new Texture(
				Gdx.files.internal("HealthBarsQi.png"));
		textureHealthBarsQiofPlayer1.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);
		textureHealthBarsShenofPlayer1 = new Texture(
				Gdx.files.internal("HealthBarsShen.png"));
		textureHealthBarsShenofPlayer1.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);

		textureHealthBarsJingofPlayer2 = new Texture(
				Gdx.files.internal("HealthBarsJing.png"));
		textureHealthBarsJingofPlayer2.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);
		textureHealthBarsQiofPlayer2 = new Texture(
				Gdx.files.internal("HealthBarsQi.png"));
		textureHealthBarsQiofPlayer2.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);
		textureHealthBarsShenofPlayer2 = new Texture(
				Gdx.files.internal("HealthBarsShen.png"));
		textureHealthBarsShenofPlayer2.setFilter(TextureFilter.Linear,
				TextureFilter.Linear);

		// the symagic draw pane
		gestureDrawPane = new SymagicGestureView("gestureDrawPane",
				gestureLogic,0.8f);
		gestureDrawPane.setLineWeight(3);
		/*Action fadeActiongestureDrawPane = FadeTo.$(0.5f, 0f);

		gestureDrawPane.action(fadeActiongestureDrawPane);*/

		gestureDrawPane.setAnimationDuringTime(0.8f);


		// player2
		player2 = new PlayerActor("player2", texturePlayer2,this);
		player2.x = 290 * xScaleRadio;
		player2.y = 635 * yScaleRadio;
		player2.width = player2.width * xScaleRadio;
		player2.height = player2.height * yScaleRadio;
		//mStage.addActor(player2);
		// player1
		player1 = new PlayerActor("player1",texturePlayer1,this);
		player1.x = 40 * xScaleRadio;
		player1.y = 25 * yScaleRadio;
		player1.width = player1.width * xScaleRadio;
		player1.height = player1.height * yScaleRadio;
		//mStage.addActor(player1);
        
		// HealthBars for player1
		// jing
		imageHealthBarsBackgroundJing1 = new Image("HealthBarsBackground",
				textureHealthBarsBackground);
		imageHealthBarsBackgroundJing1.x = 150 * xScaleRadio;
		imageHealthBarsBackgroundJing1.y = 35 * yScaleRadio;
		imageHealthBarsBackgroundJing1.width = imageHealthBarsBackgroundJing1.width
				* xScaleRadio;
		imageHealthBarsBackgroundJing1.height = imageHealthBarsBackgroundJing1.height
				* yScaleRadio;
		Action fadeActionHealthBarsBackgroundJing1 = FadeTo.$(0.5f, 0f);
		imageHealthBarsBackgroundJing1
				.action(fadeActionHealthBarsBackgroundJing1);
		mStage.addActor(imageHealthBarsBackgroundJing1);
		imageHealthBarsJingofPlayer1 = new Image("HealthBarsJingofPlayer1",
				textureHealthBarsJingofPlayer1);
		imageHealthBarsJingofPlayer1.x = 150 * xScaleRadio;
		imageHealthBarsJingofPlayer1.y = 35 * yScaleRadio;
		imageHealthBarsJingofPlayer1.width = imageHealthBarsJingofPlayer1.width
				* xScaleRadio;
		imageHealthBarsJingofPlayer1.height = imageHealthBarsJingofPlayer1.height
				* yScaleRadio;
		Action fadeActionHealthBarsJingofPlayer1 = FadeTo.$(0.5f, 0f);
		imageHealthBarsJingofPlayer1.action(fadeActionHealthBarsJingofPlayer1);
		mStage.addActor(imageHealthBarsJingofPlayer1);
		// Qi
		imageHealthBarsBackgroundQi1 = new Image("HealthBarsBackground",
				textureHealthBarsBackground);
		imageHealthBarsBackgroundQi1.x = 150 * xScaleRadio;
		imageHealthBarsBackgroundQi1.y = 55 * yScaleRadio;
		imageHealthBarsBackgroundQi1.width = imageHealthBarsBackgroundQi1.width
				* xScaleRadio;
		imageHealthBarsBackgroundQi1.height = imageHealthBarsBackgroundQi1.height
				* yScaleRadio;
		Action fadeActionHealthBarsBackgroundQi1 = FadeTo.$(0.5f, 0f);
		imageHealthBarsBackgroundQi1.action(fadeActionHealthBarsBackgroundQi1);
		mStage.addActor(imageHealthBarsBackgroundQi1);
		imageHealthBarsQiofPlayer1 = new Image("HealthBarsQiofPlayer1",
				textureHealthBarsQiofPlayer1);
		imageHealthBarsQiofPlayer1.x = 150 * xScaleRadio;
		imageHealthBarsQiofPlayer1.y = 55 * yScaleRadio;
		imageHealthBarsQiofPlayer1.width = imageHealthBarsQiofPlayer1.width
				* xScaleRadio;
		imageHealthBarsQiofPlayer1.height = imageHealthBarsQiofPlayer1.height
				* yScaleRadio;
		Action fadeActionHealthBarsQiofPlayer1 = FadeTo.$(0.5f, 0f);
		imageHealthBarsQiofPlayer1.action(fadeActionHealthBarsQiofPlayer1);
		mStage.addActor(imageHealthBarsQiofPlayer1);
		// Shen
		imageHealthBarsBackgroundShen1 = new Image("HealthBarsBackground",
				textureHealthBarsBackground);
		imageHealthBarsBackgroundShen1.x = 150 * xScaleRadio;
		imageHealthBarsBackgroundShen1.y = 75 * yScaleRadio;
		imageHealthBarsBackgroundShen1.width = imageHealthBarsBackgroundShen1.width
				* xScaleRadio;
		imageHealthBarsBackgroundShen1.height = imageHealthBarsBackgroundShen1.height
				* yScaleRadio;
		Action fadeActionHealthBarsBackgroundShen1 = FadeTo.$(0.5f, 0f);
		imageHealthBarsBackgroundShen1
				.action(fadeActionHealthBarsBackgroundShen1);
		mStage.addActor(imageHealthBarsBackgroundShen1);
		imageHealthBarsShenofPlayer1 = new Image("xuetiaoShenofPlayer1",
				textureHealthBarsShenofPlayer1);
		imageHealthBarsShenofPlayer1.x = 150 * xScaleRadio;
		imageHealthBarsShenofPlayer1.y = 75 * yScaleRadio;
		imageHealthBarsShenofPlayer1.width = imageHealthBarsShenofPlayer1.width
				* xScaleRadio;
		imageHealthBarsShenofPlayer1.height = imageHealthBarsShenofPlayer1.height
				* yScaleRadio;
		Action fadeActionHealthBarsShenofPlayer1 = FadeTo.$(0.5f, 0f);
		imageHealthBarsShenofPlayer1.action(fadeActionHealthBarsShenofPlayer1);
		mStage.addActor(imageHealthBarsShenofPlayer1);

		// HealthBars for player2
		// Jing
		imageHealthBarsBackgroundJing2 = new Image("HealthBarsBackground",
				textureHealthBarsBackground);
		imageHealthBarsBackgroundJing2.x = 60 * xScaleRadio;
		imageHealthBarsBackgroundJing2.y = 670 * yScaleRadio;
		imageHealthBarsBackgroundJing2.width = imageHealthBarsBackgroundJing2.width
				* xScaleRadio;
		imageHealthBarsBackgroundJing2.height = imageHealthBarsBackgroundJing2.height
				* yScaleRadio;
		Action fadeActionHealthBarsBackgroundJing2 = FadeTo.$(0.5f, 0f);
		imageHealthBarsBackgroundJing2
				.action(fadeActionHealthBarsBackgroundJing2);
		mStage.addActor(imageHealthBarsBackgroundJing2);
		imageHealthBarsJingofPlayer2 = new Image("HealthBarsJingofPlayer2",
				textureHealthBarsJingofPlayer2);
		imageHealthBarsJingofPlayer2.x = 60 * xScaleRadio;
		imageHealthBarsJingofPlayer2.y = 670 * yScaleRadio;
		imageHealthBarsJingofPlayer2.width = imageHealthBarsJingofPlayer2.width
				* xScaleRadio;
		imageHealthBarsJingofPlayer2.height = imageHealthBarsJingofPlayer2.height
				* yScaleRadio;
		Action fadeActionHealthBarsJingofPlayer2 = FadeTo.$(0.5f, 0f);
		imageHealthBarsJingofPlayer2.action(fadeActionHealthBarsJingofPlayer2);
		mStage.addActor(imageHealthBarsJingofPlayer2);
		// Qi
		imageHealthBarsBackgroundQi2 = new Image("HealthBarsBackground",
				textureHealthBarsBackground);
		imageHealthBarsBackgroundQi2.x = 60 * xScaleRadio;
		imageHealthBarsBackgroundQi2.y = 690 * yScaleRadio;
		imageHealthBarsBackgroundQi2.width = imageHealthBarsBackgroundQi2.width
				* xScaleRadio;
		imageHealthBarsBackgroundQi2.height = imageHealthBarsBackgroundQi2.height
				* yScaleRadio;
		Action fadeActionHealthBarsBackgroundQi2 = FadeTo.$(0.5f, 0f);
		imageHealthBarsBackgroundQi1.action(fadeActionHealthBarsBackgroundQi2);
		mStage.addActor(imageHealthBarsBackgroundQi2);
		imageHealthBarsQiofPlayer2 = new Image("HealthBarsQiofPlayer2",
				textureHealthBarsQiofPlayer2);
		imageHealthBarsQiofPlayer2.x = 60 * xScaleRadio;
		imageHealthBarsQiofPlayer2.y = 690 * yScaleRadio;
		imageHealthBarsQiofPlayer2.width = imageHealthBarsQiofPlayer2.width
				* xScaleRadio;
		imageHealthBarsQiofPlayer2.height = imageHealthBarsQiofPlayer2.height
				* yScaleRadio;
		Action fadeActionHealthBarsQiofPlayer2 = FadeTo.$(0.5f, 0f);
		imageHealthBarsQiofPlayer2.action(fadeActionHealthBarsQiofPlayer2);
		mStage.addActor(imageHealthBarsQiofPlayer2);
		// Shen
		imageHealthBarsBackgroundShen2 = new Image("HealthBarsBackground",
				textureHealthBarsBackground);
		imageHealthBarsBackgroundShen2.x = 60 * xScaleRadio;
		imageHealthBarsBackgroundShen2.y = 710 * yScaleRadio;
		imageHealthBarsBackgroundShen2.width = imageHealthBarsBackgroundShen2.width
				* xScaleRadio;
		imageHealthBarsBackgroundShen2.height = imageHealthBarsBackgroundShen2.height
				* yScaleRadio;
		Action fadeActionHealthBarsBackgroundShen2 = FadeTo.$(0.5f, 0f);
		imageHealthBarsBackgroundShen2
				.action(fadeActionHealthBarsBackgroundShen2);
		mStage.addActor(imageHealthBarsBackgroundShen2);
		imageHealthBarsShenofPlayer2 = new Image("xuetiaoShenofPlayer2",
				textureHealthBarsShenofPlayer2);
		imageHealthBarsShenofPlayer2.x = 60 * xScaleRadio;
		imageHealthBarsShenofPlayer2.y = 710 * yScaleRadio;
		imageHealthBarsShenofPlayer2.width = imageHealthBarsShenofPlayer2.width
				* xScaleRadio;
		imageHealthBarsShenofPlayer2.height = imageHealthBarsShenofPlayer2.height
				* yScaleRadio;
		Action fadeActionHealthShen2 = FadeTo.$(0.5f, 0f);
		imageHealthBarsShenofPlayer2.action(fadeActionHealthShen2);
		mStage.addActor(imageHealthBarsShenofPlayer2);


		// Setup button "Back"
		buttonTexture = new Texture(Gdx.files.internal("returnButton.png"));
		NinePatch n2 = new NinePatch(buttonTexture, 7, 7, 9, 9);
		BitmapFont bitmapFont2 = new BitmapFont();


		buttonBack = new Button("", new ButtonStyle(n2, n2, n2, 10f, 0f, 0f,
				0f, bitmapFont2, new Color(1, 1, 0, 0.5f)), "buttonBack");

		buttonBack.setClickListener(new ClickListener() {
			public void click(Actor actor) {
				game.getActivity().finish();
			}
		});

		buttonBack.x = 0 * xScaleRadio;
		buttonBack.y = 720 * yScaleRadio;
		buttonBack.width = 80f * xScaleRadio;
		buttonBack.height = 80f * yScaleRadio;

		mStage.addActor(buttonBack);

		mStage.addActor(gestureDrawPane);
		
		mStage.addActor(player2);
		
		mStage.addActor(player1);

		Gdx.input.setInputProcessor(mStage);
	}

	public void dispose() {
		// TODO Auto-generated method stub
		// stage.dispose();

	}

	public void pause() {
		// TODO Auto-generated method stub

	}



	public void render(float deltaTime) {

		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		mSpriteBatch.begin();
		mSpriteBatch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(),
				Gdx.graphics.getHeight());
		mSpriteBatch.end();

		mStage.act(Gdx.graphics.getDeltaTime());
		mStage.draw();

	}

	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	public void resume() {
		// TODO Auto-generated method stub


	}

	public boolean attackEffect(int attack) {

		if (attack != 0) {
			textureStar = new Texture(Gdx.files.internal(game.getActivity()
					.attackway(attackChar)));
			textureStar.setFilter(TextureFilter.Linear, TextureFilter.Linear);

			float maxwidth = Gdx.graphics.getWidth() - textureStar.getWidth()
					* xScaleRadio;
			float maxheight = Gdx.graphics.getHeight()
					- textureStar.getHeight() * yScaleRadio;

			imagePlayer4 = new Image("star", textureStar);
			imagePlayer4.x = 290 * xScaleRadio;
			imagePlayer4.y = 635 * yScaleRadio;
			imagePlayer4.width = imagePlayer4.width * xScaleRadio;
			imagePlayer4.height = imagePlayer4.height * yScaleRadio;
			Action moveAction = MoveTo.$(0 * xScaleRadio, 0 * yScaleRadio,
					ANIMATION_DURATION / 2);
			Action rotateAction = RotateTo.$(360, ANIMATION_DURATION);
			Action fadeAction2 = FadeTo.$(0f, 1f); // disappear in 1s
			fadeAction2.setCompletionListener(new OnActionCompleted() {
				public void completed(Action arg0) {
					// TODO Auto-generated method stub
					Action scaleActionHealthBarsJingofPlayer1 = ScaleTo.$(
							(1 - totalDamage / 100.0f), 1f, 0);
					imageHealthBarsJingofPlayer1
							.action(scaleActionHealthBarsJingofPlayer1);
				}
			});
			imagePlayer4.action(Parallel.$(moveAction, rotateAction,
					Delay.$(fadeAction2, ANIMATION_DURATION / 2)));
			mStage.addActor(imagePlayer4);
		}
		return true;
	}
	

}
